package root.arrayQueue;

import root.Queue;


public class ArrayQueue<E> implements
	Queue<E>{
	//indica primeiro item da fila
	private int f = 0;
	//indica proxima celula livre
	private int r = 0;
	//VETOR-ARRAY
	private E[] Q;
	//Tamanho real maximo da fila
	private int N;
	private final static int CAPACITY = 1000;
	
	public ArrayQueue(){
		this(CAPACITY);
	}
	public ArrayQueue(int capacity){
		this.N = capacity;
		Q = (E[]) new Object[capacity];
	}
	
	public E dequeue() throws EmptyQueueException {
		if(isEmpty())
			throw new EmptyQueueException("Fila Vazia");
		E temp = Q[f];
		Q[f] = null;
		f = (f+1)%N;
		return temp;
	}
	public void enqueue(E element) throws FullQueueException {
		if(size() == N)
			throw new FullQueueException("Fila Cheia");
		Q[r] = element;
		r = (r+1) % N;
	}
	public E front() throws EmptyQueueException {
		if(isEmpty())
			throw new EmptyQueueException("Fila Vazia");
		return Q[f];
	}
	public boolean isEmpty() {
		return (f == r);
	}
	public int size() {
		return (N - f + r) % N;
	}
}
